<?php

namespace app\push\controller;

use app\admin\model\kaoq\Course;
use app\admin\model\kaoq\Kaoqin;
//use app\push\model\Qiandao;
use app\admin\model\kaoq\Student;
use GatewayWorker\Lib\Gateway;
use think\Db;

class Events
{
    /**
     * 有消息时
     * @param integer $client_id 连接的客户端
     * @param mixed $message
     * @return void
     */
    public static function onMessage($client_id, $message)
    {
        // 向所有人发送
        echo "接收到的数据是:".$message."\n";
        $message_data = (array)json_decode($message,true);
        if($message_data){
            switch ($message_data['type']){
                case "init":
                    Gateway::bindUid($client_id,$message_data['pid']);
                    echo "绑定状态\n";
                    break;
                case "work":
                    //整体缓存
                  echo "working\n";
//                    $studentName =  Student::where('student_id','=',$message_data['student'])->find()['name'];
//                    Gateway::sendToUid($message_data['course_id'],$studentName.'-学号:'.$message_data['student']);
//                    break;
                  $status = cache($message_data['course_id'].$message_data['student']);
                  if(!$status){
                      //上课班级号缓存
                      $class_id =cache('class_id'.$message_data['course_id']);
                      if(!$class_id) {
                          $class_id = Course::get($message_data['course_id'])['class_id'];
                          cache('class_id' . $message_data['course_id'], $class_id);
                          $studentcache = Student::where('grade_id','=',$class_id)->where('student_id','=',$message_data['student'])->find();
                          if($studentcache){
                              $res=  Kaoqin::create(['course_id'=>$message_data['course_id'],'student_id'=>$message_data['student']]);
                              if($res){
                                  $studentName =  Student::where('student_id','=',$message_data['student'])->find()['name'];
                                  Gateway::sendToUid($message_data['course_id'],$studentName.'-学号:'.$message_data['student']);
                                  cache($message_data['course_id'].$message_data['student'],'1');
                                  echo "缓存成功-课程号：".$message_data['course_id'].'-学号：'.$message_data['student']."\n";
                              }
                          }
                      }
                      else{
                          $studentcache = Student::where('grade_id','=',$class_id)->where('student_id','=',$message_data['student'])->find();
                          if($studentcache){
                              $res=  Kaoqin::create(['course_id'=>$message_data['course_id'],'student_id'=>$message_data['student']]);
                              if($res){
                                  $studentName =  Student::where('student_id','=',$message_data['student'])->find()['name'];
                                  Gateway::sendToUid($message_data['course_id'],$studentName.'-学号:'.$message_data['student']);
                                  cache($message_data['course_id'].$message_data['student'],'1');
                                  echo "缓存成功-课程号：".$message_data['course_id'].'-学号：'.$message_data['student']."\n";
                              }
                          }
                      }
                  }
                  break;
            }
        }
    }



    /**
     * 当用户连接时触发的方法
     * @param integer $client_id 连接的客户端
     * @return void
     */
    public static function onConnect($client_id)
    {
//        Gateway::sendToAll($client_id);
        echo "client:{$_SERVER['REMOTE_ADDR']}:{$_SERVER['REMOTE_PORT']} gateway:{$_SERVER['GATEWAY_ADDR']}:{$_SERVER['GATEWAY_PORT']}  client_id:$client_id session:".json_encode($_SESSION)."\n";
    }

    /**
     * 当用户断开连接时触发的方法
     * @param integer $client_id 断开连接的客户端
     * @return void
     */
    public static function onClose($client_id)
    {
//        Gateway::sendToAll("client[$client_id] logout\n");
    }

    /**
     * 当进程启动时
     * @param integer $businessWorker 进程实例
     */
    public static function onWorkerStart($businessWorker)
    {
        echo "WorkerStart\n";
    }

    /**
     * 当进程关闭时
     * @param integer $businessWorker 进程实例
     */
    public static function onWorkerStop($businessWorker)
    {
        echo "WorkerStop\n";
    }
}